package marking.liu.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.benmanes.caffeine.cache.Cache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import marking.liu.config.CaffeineCacheConfig;
import marking.liu.pojo.domain.GainerDataDomain;
import marking.liu.pojo.domain.InnerMarketDomain;
import marking.liu.pojo.domain.SectorIndicesDomain;
import marking.liu.pojo.domain.Stock4MinuteDomain;
import marking.liu.pojo.vo.PageResult;
import marking.liu.pojo.vo.Result;
import marking.liu.service.StockService;
import marking.liu.utils.DateTimeUtil;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/api")
@Api(tags = "股票接口资源")
public class StockController {

    @Autowired
    private StockService stockService;
    @Autowired
    private Cache caffeineCache;

    /**
     * 获取最新国内A股大盘信息（仅包含上证和深证大盘数据）
     * @return
     */

    @GetMapping("/quot/index/all")
    @ApiOperation("获取最新国内A股大盘信息（仅包含上证和深证大盘数据）")
    public Result<List<InnerMarketDomain>> getInnerMarketInfo() {
//        优先获取缓存中的数据
        Result<List<InnerMarketDomain>> innerMarketDomainList1 = (Result<List<InnerMarketDomain>>) caffeineCache.get("innerMarketInfos", key -> {
            //如果不存在，则从数据库查询
            List<InnerMarketDomain> innerMarketDomainList = stockService.getInnerMarketInfo();
            log.info("返回数据成功:{}", innerMarketDomainList);
            return Result.ok(innerMarketDomainList);
//            return null;
        });
//        JSONObject.parseArray()
        log.info("缓存的数据为:{}", innerMarketDomainList1);
        return innerMarketDomainList1;
    }

    /**
     * 获取板块指数
     * @return
     */
    @GetMapping("/quot/sector/all")
    @ApiOperation("获取板块指数")
    public Result<List<SectorIndicesDomain>> getSectorIndicesInfo() {
        List<SectorIndicesDomain> sectorIndicesDomainList = stockService.getSectorIndicesInfo();
        return Result.ok(sectorIndicesDomainList);
    }

    /**
     * 分页查询最新股票交易时间点下沪深的行情数据
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/quot/stock/all")
    @ApiOperation("分页查询最新股票交易时间点下沪深的行情数据")
    public Result<PageResult> getStockPageInfo(@RequestParam(required = false, defaultValue = "1") Integer page,
                                               @RequestParam(required = false, defaultValue = "20") Integer pageSize)
    {
        PageResult pageResult = stockService.getStockPageInfo(page, pageSize);
        return Result.ok(pageResult);
    }
    /**
     * 涨幅榜数据
     */
    @GetMapping("/quot/stock/increase")
    @ApiOperation("涨幅榜数据")
    public Result<List<GainerDataDomain>> getIncreaseInfo(@RequestParam(required = false, defaultValue = "1") Integer page,
                                                    @RequestParam(required = false, defaultValue = "4") Integer pageSize
    )
    {
        List<GainerDataDomain> gainerDataDomainList = stockService.getIncreaseInfo(page, pageSize);
        return Result.ok(gainerDataDomainList);
    }

    /**
     * 涨停跌停表信息
     * @return
     */
    @GetMapping("/quot/stock/updown/count")
    @ApiOperation("涨停跌停表")
    public Result<Map<String, List>> getStockUpdownCount() {
        Map<String, List> stringListMap =  stockService.getStockUpdownCount();
        return Result.ok(stringListMap);
    }

    /**
     * 导出涨停跌停excel表
     * @param response
     * @param page
     * @param pageSize
     */
    @GetMapping("/quot/stock/export")
    public void exportExcel(HttpServletResponse response,
                            @RequestParam(required = false, defaultValue = "1") Integer page,
                            @RequestParam(required = false, defaultValue = "20") Integer pageSize) throws IOException {
        stockService.exportExcel(response, page, pageSize);
    }

    /**
     * 统计国内A股大盘T日和T-1日成交量对比功能（成交量为沪市和深市成交量之和）
     * @return
     */
    @GetMapping("/quot/stock/tradeAmt")
    public Result<Map> stockTradeVol4InnerMarket(){
        Map stockMap =  stockService.stockTradeVol4InnerMarket();
        return Result.ok(stockMap);
    }

    /**
     * 查询单个个股的分时行情数据，也就是统计指定股票T日每分钟的交易数据；
     * 如果当前日期不在有效时间内，则以最近的一个股票交易时间作为查询时间点
     * @param code
     * @return
     */
    @GetMapping("/quot/stock/screen/time-sharing")
    public Result<List<Stock4MinuteDomain>> stockScreenTimeSharing (@RequestParam String code) {
        List<Stock4MinuteDomain>  stock4MinuteDomains =  stockService.stockScreenTimeSharing(code);
        return Result.ok(stock4MinuteDomains);
    }
}
